package com.meteor.downloader.videodownloader.admdm

import androidx.appcompat.app.AppCompatActivity
import com.meteor.downloader.videodownloader.MdmDealApplication
import com.meteor.downloader.videodownloader.admdm.AdInfoUtils.MDM_SHOW_AD_LIST
import com.meteor.downloader.videodownloader.admdm.AdInfoUtils.mMdmShowAdList
import com.meteor.downloader.videodownloader.mdmBaseResult64
import com.meteor.downloader.videodownloader.utils.ApplicationMdmUtils
import com.meteor.downloader.videodownloader.utils.DataManagerMdmUtils
import com.meteor.downloader.videodownloader.utils.MdmTotalUtils
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types

object MdmDealAdUtils {
    private val mListOpenValue = mutableListOf<ValueMdmInfo>()
    private val mListNativeValue = mutableListOf<ValueMdmInfo>()
    private val mListIntersValue = mutableListOf<ValueMdmInfo>()
    private fun initAdUtils() {
        if (mListOpenValue.isEmpty() || mListIntersValue.isEmpty()) {
            val value =
                DataManagerMdmUtils.getDataKeyMdm(MDM_SHOW_AD_LIST, mMdmShowAdList).mdmBaseResult64()
            val moshi = Moshi.Builder().build()
            val typeData = Types.newParameterizedType(
                MutableList::class.java, ValueMdmInfo::class.java
            )
            moshi.adapter<MutableList<ValueMdmInfo>>(typeData).fromJson(value)?.let {
                for (data in it) {
                    if (data.mdmAdType == "mdmOpen" || data.mdmAdType == "mdmIntersOpen") {
                        mListOpenValue.add(data)
                    }
                    if (data.mdmAdType == "mdmNamdmve") {
                        mListNativeValue.add(data)
                    }
                    if (data.mdmAdType == "mdmInters") {
                        mListIntersValue.add(data)
                    }
                }
            }
        }
    }

    private var mBookOpenLoading = false
    fun loadBookOpenMdm(isAll: Boolean = true) {
        if (mBookOpenLoading) {
            return
        }
        mBookOpenLoading = true
        initAdUtils()
        if (mListOpenValue.isEmpty()) {
            mBookOpenLoading = false
            MdmDealApplication.mMdmRootActivity?.loadBookOpenMdm(false)
            return
        }
        loadBookOpenMdm2(isAll, 0)
    }

    private fun loadBookOpenMdm2(isAll: Boolean = true, index: Int = 0) {
        try {
            val maxSize = if (isAll) {
                mListOpenValue.size
            } else {
                mListOpenValue.size - 1
            }
            if (index >= maxSize) {
                mBookOpenLoading = false
                MdmDealApplication.mMdmRootActivity?.loadBookOpenMdm(false)
                return
            }
            val adData = mListOpenValue[index]
            if (!adData.mBookShowing && adData.mOpenObject != null) {
                if (System.currentTimeMillis() - adData.mLoadTime >= 4 * 3600 * 1000) {
                    adData.changeResultToNull()
                } else {
                    mBookOpenLoading = false
                    MdmDealApplication.mMdmRootActivity?.loadBookOpenMdm(true)
                    return
                }
            }
            if (adData.mdmAdType == "mdmOpen") {
                BookOpenUtils.loadTIAdInfo(adData) {
                    if (it) {
                        mBookOpenLoading = false
                        MdmDealApplication.mMdmRootActivity?.loadBookOpenMdm(true)
                    } else {
                        loadBookOpenMdm2(isAll, index + 1)
                    }
                }
            } else {
                BookIntersUtils.loadTIAdInfo(adData) {
                    if (it) {
                        mBookOpenLoading = false
                        MdmDealApplication.mMdmRootActivity?.loadBookOpenMdm(true)
                    } else {
                        loadBookOpenMdm2(isAll, index + 1)
                    }
                }
            }
        } catch (_: Exception) {
            mBookOpenLoading = false
            MdmDealApplication.mMdmRootActivity?.loadBookOpenMdm(false)
        }
    }

    fun showTionOpenValue(activity: AppCompatActivity, listener: ShowPageAdListener) {
        getVlaueForOpen()?.let {
            if (it.mOpenObject != null) {
                BookOpenUtils.showMdmAdValue(activity, it, listener)
            } else if (it.mIntersObject != null) {
                BookIntersUtils.showMdmAdValue(activity, it, false, listener)
            } else {
                listener.dismissAFailed()
            }
        } ?: run {
            listener.dismissAFailed()
        }
    }


    private var mBookIntersLoading = false
    fun loadBookIntersMdm(isAll: Boolean = true, mStatusLook: Boolean = true) {
        if (mBookIntersLoading) {
            return
        }
        mBookIntersLoading = true
        if (mStatusLook) {
            if (!MdmTotalUtils.getMdmAppStatus()) {
                mBookIntersLoading = false
                MdmDealApplication.mMdmRootActivity?.loadBookIntersMdm(false)
                return
            }
        }
        initAdUtils()
        if (mListIntersValue.isEmpty()) {
            mBookIntersLoading = false
            MdmDealApplication.mMdmRootActivity?.loadBookIntersMdm(false)
            return
        }
        loadTIIntersA2(isAll, 0)
    }

    private fun loadTIIntersA2(isAll: Boolean = true, index: Int = 0) {
        try {
            val maxSize = if (isAll) {
                mListIntersValue.size
            } else {
                mListIntersValue.size - 1
            }
            if (index >= maxSize) {
                mBookIntersLoading = false
                MdmDealApplication.mMdmRootActivity?.loadBookIntersMdm(false)
                return
            }
            val adData = mListIntersValue[index]
            if (!adData.mBookShowing && adData.mIntersObject != null) {
                if (System.currentTimeMillis() - adData.mLoadTime >= 4 * 3600 * 1000) {
                    adData.changeResultToNull()
                } else {
                    mBookIntersLoading = false
                    MdmDealApplication.mMdmRootActivity?.loadBookIntersMdm(true)
                    return
                }
            }
            BookIntersUtils.loadTIAdInfo(adData) {
                if (it) {
                    mBookIntersLoading = false
                    MdmDealApplication.mMdmRootActivity?.loadBookIntersMdm(true)
                } else {
                    loadTIIntersA2(isAll, index + 1)
                }
            }
        } catch (e: Exception) {
            mBookIntersLoading = false
            MdmDealApplication.mMdmRootActivity?.loadBookIntersMdm(false)
        }
    }

    fun showTionIntersValue(
        activity: AppCompatActivity,
        type: String,
        mStatusLook: Boolean = true,
        listener: ShowPageAdListener
    ) {
        if (mStatusLook) {
            if (!MdmTotalUtils.getMdmAppStatus()) {
                listener.dismissAFailed()
                return
            }
        }
        if (type.isNotEmpty()) {
            when (type) {
                "mdmBack" -> {
                    if (!ApplicationMdmUtils.mShowPageBack) {
                        listener.dismissAFailed()
                        return
                    }
                }

                "mdmEnt" -> {
                    if (!ApplicationMdmUtils.mShowPageEnter) {
                        listener.dismissAFailed()
                        return
                    }
                }
            }
        }
        getVlaueForInters()?.let {
            if (it.mIntersObject != null) {
                BookIntersUtils.showMdmAdValue(activity, it, mStatusLook, listener)
            } else {
                listener.dismissAFailed()
            }
        } ?: run {
            listener.dismissAFailed()
        }
    }
    //----------------------------------------------------------------------------------------------------------

    private var mBookNativeLoading = false
    fun loadBookNativeMdm(isAll: Boolean = true, mStatusLook: Boolean = false) {
        if (mBookNativeLoading) {
            return
        }
        mBookNativeLoading = true
        if (mStatusLook) {
            if (!MdmTotalUtils.getMdmAppStatus()) {
                mBookNativeLoading = false
                MdmDealApplication.mMdmRootActivity?.loadBookNativeMdm(false)
                return
            }
        }
        initAdUtils()
        if (mListNativeValue.isEmpty()) {
            mBookNativeLoading = false
            MdmDealApplication.mMdmRootActivity?.loadBookNativeMdm(false)
            return
        }
        loadBookNativeMdm2(isAll, 0)
    }

    private fun loadBookNativeMdm2(isAll: Boolean = true, index: Int = 0) {
        try {
            val maxSize = if (isAll) {
                mListNativeValue.size
            } else {
                mListNativeValue.size - 1
            }
            if (index >= maxSize) {
                mBookNativeLoading = false
                MdmDealApplication.mMdmRootActivity?.loadBookNativeMdm(false)
                return
            }
            val adData = mListNativeValue[index]
            if (!adData.mBookShowing && adData.mNativeObject != null) {
                if (System.currentTimeMillis() - adData.mLoadTime >= 1 * 3600 * 1000) {
                    adData.changeResultToNull()
                } else {
                    mBookNativeLoading = false
                    MdmDealApplication.mMdmRootActivity?.loadBookNativeMdm(true)
                    return
                }
            }
            BookNativeUtils.loadTIAdInfo(adData) {
                if (it) {
                    mBookNativeLoading = false
                    MdmDealApplication.mMdmRootActivity?.loadBookNativeMdm(true)
                } else {
                    loadBookNativeMdm2(isAll, index + 1)
                }
            }
        } catch (e: Exception) {
            mBookNativeLoading = false
            MdmDealApplication.mMdmRootActivity?.loadBookNativeMdm(false)
        }
    }

    fun showTionNativeValue(
        activity: AppCompatActivity,
        nativeType: String,
        mStatusLook: Boolean = false,
        listener: ShowPageAdListener
    ) {
        if (mStatusLook) {
            if (!MdmTotalUtils.getMdmAppStatus()) {
                listener.nativeAddShowBack(null, null)
                return
            }
        }
        getVlaueForNative()?.let {
            if (it.mNativeObject != null) {
                BookNativeUtils.showMdmAdValue(activity, nativeType, it, listener)
            } else {
                listener.nativeAddShowBack(null, null)
            }
        } ?: run {
            listener.nativeAddShowBack(null, null)
        }
    }

    //=======================================================================================

    private fun getVlaueForOpen(): ValueMdmInfo? {
        for (item in mListOpenValue) {
            if (!item.mBookShowing && (item.mOpenObject != null || item.mIntersObject != null)) {
                return item
            }
        }
        return null
    }

    fun getVlaueForInters(): ValueMdmInfo? {
        for (item in mListIntersValue) {
            if (!item.mBookShowing && item.mIntersObject != null) {
                return item
            }
        }
        return null
    }

    private fun getVlaueForNative(): ValueMdmInfo? {
        for (item in mListNativeValue) {
            if (!item.mBookShowing && item.mNativeObject != null) {
                return item
            }
        }
        return null
    }
}